package com.yzr.message.util;


import com.yzr.message.model.MessageBody;
import org.springframework.util.StringUtils;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Properties;

/**
 * @description
 * @author: yzr
 * @date: 2021/12/31 14:24
 **/
public class SendEmail {

    private static final String PROPERTIES_AUTH = "mail.smtp.auth";

    private static final String PROPERTIES_HOST = "mail.smtp.host";

    private static final String PRIVATE_KEY = "FGHVGULGNAVACOFF";

    private String host;

    private String sender;

    private String receiver;

    private String senderPassword;

    private Session session;


    /**
     * @param host           使用的服务器地址
     * @param sender         发送人（邮箱）
     * @param senderPassword 发送人的密码
     * @param receiver       接收人
     */
    public SendEmail(String host, String sender, String senderPassword, String receiver) {
        this.host = host;
        this.sender = sender;
        this.receiver = receiver;
        this.senderPassword = senderPassword;

        this.initSession();
    }

    private void initSession() {
        Properties properties = System.getProperties();
        properties.setProperty(PROPERTIES_HOST, this.host);
        properties.setProperty(PROPERTIES_AUTH, Boolean.TRUE.toString());

        this.session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sender, senderPassword);
            }
        });
    }

    public boolean send(MessageBody messageBody) throws MessagingException, FileNotFoundException {
        this.initSession();
        final MimeMessage mimeMessage = new MimeMessage(this.session);

        if (!StringUtils.hasLength(messageBody.getHeader())) {
            throw new MessagingException("发送邮件的标题不可为空");
        }

        if (StringUtils.hasLength(messageBody.getContent())) {
            mimeMessage.setText(messageBody.getContent());
        }

        if (StringUtils.hasLength(messageBody.getAttachment())) {
            if (!new File(messageBody.getAttachment()).exists()) {
                throw new FileNotFoundException("发送邮件附件失败：附件文件不存在");
            }
            mimeMessage.setFileName(messageBody.getAttachment());
        }

        mimeMessage.setSubject(messageBody.getHeader());
        mimeMessage.setFrom(new InternetAddress(sender));
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
        Transport.send(mimeMessage);
        return true;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSenderPassword() {
        return senderPassword;
    }

    public void setSenderPassword(String senderPassword) {
        this.senderPassword = senderPassword;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
